home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / application.py next >
Text File  |  2009-11-05  |  5KB  |  143 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import sys
  20. import logging
  21. import posixpath
  22.  
  23. from gettext import gettext as _
  24.  
  25. from logging import StreamHandler, FileHandler, Formatter
  26. from optparse import OptionParser
  27.  
  28. from checkbox.contrib import bpickle_registry
  29.  
  30. from checkbox.lib.config import Config
  31. from checkbox.lib.environ import get_variable
  32. from checkbox.lib.safe import safe_make_directory
  33. from checkbox.lib.text import split
  34.  
  35. from checkbox.plugin import PluginManager
  36. from checkbox.reactor import Reactor
  37. from checkbox.registry import RegistryManager
  38.  
  39.  
  40. class Application(object):
  41.  
  42.     reactor_factory = Reactor
  43.  
  44.     def __init__(self, config):
  45.         self._config = config
  46.         self.reactor = self.reactor_factory()
  47.  
  48.         # Registry manager setup
  49.         self.registry = RegistryManager(self._config)
  50.  
  51.         # Plugin manager setup
  52.         self.plugin_manager = PluginManager(self._config,
  53.             self.reactor, self.registry)
  54.  
  55.     def run(self):
  56.         try:
  57.             bpickle_registry.install()
  58.             self.reactor.run()
  59.             bpickle_registry.uninstall()
  60.         except:
  61.             logging.exception("Error running reactor.")
  62.             raise
  63.  
  64.  
  65. class ApplicationManager(object):
  66.  
  67.     application_factory = Application
  68.  
  69.     default_log_level = "critical"
  70.  
  71.     def parse_options(self, args):
  72.         usage = _("Usage: checkbox [OPTIONS]")
  73.         parser = OptionParser(usage=usage)
  74.         parser.add_option("--version",
  75.                           action="store_true",
  76.                           help=_("Print version information and exit."))
  77.         parser.add_option("-l", "--log",
  78.                           metavar="FILE",
  79.                           help=_("The file to write the log to."))
  80.         parser.add_option("--log-level",
  81.                           default=self.default_log_level,
  82.                           help=_("One of debug, info, warning, error or critical."))
  83.         parser.add_option("-c", "--config",
  84.                           action="append",
  85.                           type="string",
  86.                           default=[],
  87.                           help=_("Configuration override parameters."))
  88.         return parser.parse_args(args)
  89.  
  90.     def create_application(self, args=sys.argv):
  91.         # Create data directory
  92.         data_directory = get_variable("CHECKBOX_DATA", ".")
  93.         safe_make_directory(data_directory)
  94.  
  95.         # Prepend environment options
  96.         string_options = get_variable("CHECKBOX_OPTIONS", "")
  97.         args[:0] = split(string_options)
  98.         (options, args) = self.parse_options(args)
  99.  
  100.         log_level = logging.getLevelName(options.log_level.upper())
  101.         log_handlers = []
  102.         if options.log:
  103.             log_filename = options.log
  104.             log_handlers.append(FileHandler(log_filename))
  105.         else:
  106.             log_handlers.append(StreamHandler())
  107.  
  108.         # Logging setup
  109.         format = ("%(asctime)s %(levelname)-8s %(message)s")
  110.         if log_handlers:
  111.             for handler in log_handlers:
  112.                 handler.setFormatter(Formatter(format))
  113.                 logging.getLogger().addHandler(handler)
  114.             if log_level:
  115.                 logging.getLogger().setLevel(log_level)
  116.         elif not logging.getLogger().handlers:
  117.             logging.disable(logging.CRITICAL)
  118.  
  119.         # Config setup
  120.         if len(args) != 2:
  121.             sys.stderr.write(_("Missing configuration file as argument.\n"))
  122.             sys.exit(1)
  123.  
  124.         config = Config()
  125.         config_filename = posixpath.expanduser(args[1])
  126.         config.read_filename(config_filename)
  127.         config.read_configs(options.config)
  128.  
  129.         section_name = "checkbox/plugins/client_info"
  130.         section = config.get_section(section_name)
  131.         if not section:
  132.             section = config.add_section(section_name)
  133.         section.set("name", posixpath.basename(args[1]) \
  134.             .replace(".ini", ""))
  135.         section.set("version", config.get_defaults().version)
  136.  
  137.         # Check options
  138.         if options.version:
  139.             print config.get_defaults().version
  140.             sys.exit(0)
  141.  
  142.         return self.application_factory(config)
  143.